package com.example.service.Service;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.service.R;
import com.example.service.Service.MusicService;

public class StartService extends Fragment {
    public StartService(){
        super(R.layout.start_service);
    }

    public void onViewCreated(@NonNull View view, @Nullable Bundle saveInstanceState) {
        super.onViewCreated(view, saveInstanceState);
        Button playMusicButton = getView().findViewById(R.id.play);
        Button stopMusicButton = getView().findViewById(R.id.stop);

        playMusicButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MusicService.class);
                getActivity().startService(intent);
                Toast.makeText(getActivity(), "播放音乐！", Toast.LENGTH_SHORT).show();
            }
        });

        stopMusicButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MusicService.class);
                getActivity().stopService(intent);
            }
        });
    }
}
